﻿using StudentLive.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace StudentLive.InnerPages
{
    public sealed partial class PreferencesUserControl : UserControl
    {
        public PreferencesUserControl()
        {
            this.InitializeComponent();
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey("ArabicLanguage"))
                LanguageToggle.IsOn = (bool)ApplicationData.Current.LocalSettings.Values["ArabicLanguage"];

        }

        private void OnToggled(object sender, RoutedEventArgs e)
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values["ArabicLanguage"] = LanguageToggle.IsOn;

            //Get Current Page
            var currentContent = Window.Current.Content;
            var currentFrame = currentContent as Frame;
            var page = currentFrame.Content as Page;                      
            
            Button backButton = (Button)page.FindName("backButton");
            backButton.Style = Application.Current.Resources["BackButtonStyle"] as Style;

            string langId = "en";
            if ((bool)localSettings.Values["ArabicLanguage"] == true)
            {
                langId = "ar";
                backButton.Style = Application.Current.Resources["BackButtonStyleArabic"] as Style;
            }

            localSettings.Values[Constants.LanguageId] = langId;
            Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = langId;

            currentFrame.Navigate(page.GetType());
            page.FlowDirection = Constants.GetFlowDirection();


        }

    }
}
